<template>
  <div class="app-container">
    <!-- 搜索工具栏 -->
    <div class="search-bar">
      <el-form ref="queryForm" :inline="true" :model="queryParams" size="small">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input
            v-model="queryParams.deviceId"
            clearable
            placeholder="请输入设备ID"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['game:log:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['game:log:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['game:log:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 日志列表 -->
    <div v-loading="loading" class="log-list-container">
      <div v-for="(log, index) in logList" :key="index" :class="getStatusClass(log.status)" class="log-item">
        <!-- 日志基本信息 -->
        <div class="log-header" @click="toggleDetail(index)">
          <div class="log-info">
            <div class="log-title-row">
              <div class="log-title">
                <span v-if="log.game" class="game-name">{{ log.game.gameName }}</span>
              </div>
            </div>
            <div class="log-meta">
              <div v-if="log.gameDevice" class="meta-item">
                <i class="el-icon-mobile-phone"/>
                <span>{{ log.gameDevice.deviceName }}</span>
                <el-tooltip content="点击复制设备ID" placement="top">
                  <span class="device-id" @click.stop="copyDeviceId(log.deviceId)">
                    <i class="el-icon-copy-document"/>
                  </span>
                </el-tooltip>
              </div>
              <div v-if="log.gameAccount" class="meta-item">
                <i class="el-icon-user"></i>
                <span>{{ log.gameAccount.account }}</span>
              </div>
              <div class="meta-item">
                <i class="el-icon-time"></i>
                <span>{{ log.createTime }}</span>
              </div>
            </div>
          </div>
          <div class="toggle-btn">
            <i :class="activeIndex===index? 'el-icon-caret-top' : 'el-icon-caret-bottom'"></i>
          </div>
        </div>
        <transition :name="activeIndex===index? 'el-zoom-in-bottom':'el-zoom-in-top'">
          <!-- 日志详细信息 -->
          <div v-show="activeIndex === index" class="log-details">
            <div class="details-header">
              <span>详细日志信息</span>
              <span v-if="log.logDetails" class="details-count">共 {{ log.logDetails.length }} 条</span>
            </div>
            <div v-if="log.logDetails" class="details-list">
              <div v-for="(detail, detailIndex) in log.logDetails" :key="detailIndex" class="detail-item">
                <div class="detail-content">
                  <div class="detail-time">{{ detail.time }}</div>
                  <div class="detail-msg">{{ detail.msg }}</div>
                </div>
              </div>
            </div>

            <!-- 图片轮播 -->
            <div v-if="log.img" class="image-carousel">
              <div class="details-header">
                <span>日志截图</span>
                <span v-if="log.img" class="details-count">共 {{ log.img.length }} 条</span>
              </div>
              <el-carousel :autoplay="true" height="180px" indicator-position="outside" type="card">
                <el-carousel-item v-for="(img, imgIndex) in log.img" :key="imgIndex">
                  <el-image :preview-src-list="log.img" :src="img" alt="日志图片" class="carousel-image" fit="contain"/>
                </el-carousel-item>
              </el-carousel>
            </div>

            <div class="operations">
              <el-button
                v-hasPermi="['game:log:edit']"
                size="mini"
                type="text"
                @click.stop="handleUpdate(log)">修改
              </el-button>
              <el-button
                v-hasPermi="['game:log:remove']"
                size="mini"
                type="text"
                @click.stop="handleDelete(log)">删除
              </el-button>
            </div>
          </div>
        </transition>

      </div>

      <!-- 无数据提示 -->
      <div v-if="logList.length === 0 && !loading" class="no-data">
        <el-empty description="暂无日志"></el-empty>
        <el-button icon="el-icon-refresh" size="small" type="text" @click="getList">刷新</el-button>
      </div>
    </div>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账号ID"/>
        </el-form-item>
        <el-form-item label="游戏ID" prop="gameId">
          <el-input v-model="form.gameId" placeholder="请输入游戏ID"/>
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID"/>
        </el-form-item>
        <el-form-item label="结果图" prop="img">
          <el-input v-model="form.img" placeholder="请输入结果图" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addLog, delLog, getLog, listLog, updateLog} from "@/api/game/log";

export default {
  name: "Log",

  dicts: ['sys_normal_disable'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志列表数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前展开的日志索引
      activeIndex: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountId: null,
        gameId: null,
        deviceId: null,
        logDetails: null,
        img: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountId: [
          {required: true, message: "账号ID不能为空", trigger: "blur"}
        ],
        gameId: [
          {required: true, message: "游戏ID不能为空", trigger: "blur"}
        ],
        deviceId: [
          {required: true, message: "设备ID不能为空", trigger: "blur"}
        ]
      }
    };
  },

  created() {
    this.getList();
  },

  methods: {
    /** 查询日志列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 切换日志详情显示
    toggleDetail(index) {
      this.activeIndex = this.activeIndex === index ? null : index;
    },

    // 获取状态对应的类名
    getStatusClass(status) {
      return {
        'status-running': status === '0',
        'status-success': status === '1',
        'status-error': status === '2'
      };
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日志";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改日志";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLog(this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(() => {
        return delLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('game/log/export', {
        ...this.queryParams
      }, `log_${new Date().getTime()}.xlsx`);
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        accountId: null,
        gameId: null,
        deviceId: null,
        logDetails: null,
        img: null,
        status: null,
        remark: null
      };
      this.resetForm("form");
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 复制设备码
    copyDeviceId(id) {
      navigator.clipboard.writeText(id).then(() => {
        this.$message.success('设备码已复制');
      }).catch(() => {
        this.$message.error('复制失败');
      });
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
  min-width: 400px;
}

::v-deep .el-carousel__item {
  width: auto;
}

.search-bar {
  background: #fff;
  padding: 18px;
  border-radius: 4px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.mb8 {
  margin-bottom: 8px;
}

/* 新的日志列表样式 */
.log-list-container {
  width: 100%;
  margin: 16px 0;
}

.log-item {
  margin-bottom: 16px;
  border-radius: 8px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: box-shadow 0.3s ease;
  border-left: 4px solid transparent;
}

/* 状态样式 - 使用左边框颜色表示状态 */
.status-running {
  border-left-color: #409EFF; /* 正在执行 - 蓝色 */
}

.status-success {
  border-left-color: #67C23A; /* 执行完毕 - 绿色 */
}

.status-error {
  border-left-color: #F56C6C; /* 执行异常 - 红色 */
}

.log-item:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px 20px;
  cursor: pointer;
  position: relative;
}

.log-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  width: 100%;
}

.log-title {
  display: flex;
  align-items: center;
}

.game-name {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin-right: 12px;
  background-color: #f0f2f5;
  padding: 4px 8px;
  border-radius: 4px;
}

.log-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 12px;
}

.meta-item i {
  margin-right: 6px;
  font-size: 14px;
  color: #909399;
}

.device-id {
  margin-left: 8px;
  color: #409EFF;
  cursor: pointer;
}

.device-id:hover {
  opacity: 0.8;
}

.toggle-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
}

.toggle-btn i {
  color: #909399;
  font-size: 14px;
}

.log-details {
  padding: 0 20px 20px;
  background-color: #fafafa;
  border-top: 1px solid #f0f0f0;
  overflow: hidden;
  transition: all 0.3s ease;
  max-height: 800px;
}

.details-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 5px;
  font-weight: 500;
  color: #333;
}

.details-header span {
  font-size: 12px;
}

.details-count {
  font-size: 10px;
  color: #999;
  font-weight: normal;
}

.details-list {
  max-height: 300px;
  overflow-y: auto;
  border-radius: 6px;
  background-color: #fff;
  padding: 8px;
  border: 1px solid #f0f0f0;
}

.detail-item {
  padding: 8px 12px;
  border-bottom: 1px solid #f5f5f5;
  font-size: 12px;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-content {
  display: flex;
  flex-direction: column;
}

.detail-time {
  color: #999;
  margin-bottom: 4px;
  font-size: 12px;
}

.detail-msg {
  color: #333;
  line-height: 1.5;
  word-break: break-all;
}

.image-carousel {
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid #f0f0f0;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: pointer;
}

.operations {
  padding: 12px 0;
  text-align: right;
  border-top: 1px solid #ebeef5;
  margin-top: 12px;
}

.no-data {
  text-align: center;
  padding: 60px 0;
  color: #999;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  height: 100%;
}

.no-data i {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
  color: #dcdfe6;
}

.no-data p {
  margin: 0;
  font-size: 14px;
}

/* 优化动画效果，解决收起时的卡顿问题 */
.log-details {
  animation-fill-mode: both;
  animation-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
}

.log-details[style*="display: none"] {
  animation: collapse 0.3s;
}

.log-details:not([style*="display: none"]) {
  animation: expand 0.3s;
}

@keyframes expand {
  from {
    max-height: 0;
    opacity: 0;
  }
  to {
    max-height: 800px;
    opacity: 1;
  }
}

@keyframes collapse {
  from {
    max-height: 800px;
    opacity: 1;
  }
  to {
    max-height: 0;
    opacity: 0;
  }
}

</style>
